const uuid = require('node-uuid');
const dateHelper = require('./helper/dataHelper.js');
module.exports = function authContructor(app,redisHelper,config){
    /**
     * 三种用例:
     *  1. 只验证应用: appId,secret,username,deviceId
     *  2. 验证用户身份.appId,secret,secret,deviceIdusername,password
     *  3. 通过token验证用户身份appId,secret,deviceId,username,accessToken(过滤了以便请求权限)
    */
    app.post('/instant/authenticate',function(req,res,next){
        console.log('\nAuth.-------------------------------------------------------->' + dateHelper.getNowFormatTime());
        console.log('Request: request coming: ' + req.ip);
        console.log('Request body: ');
        conosle.log(req.body);

        //验证设备，唯一标识，Hash(deviceId_pushToken,push_Token)
        var deviceId = req.body.deviceId;
        var username = req.body.username;
        if(deviceId === undefined || deviceId === '' || username === undefined || username === ''){
            res.status(401).json({
                code: 401,
                status:'Unauthorized',
                message:'app validation fail,please check query params'
            })
            return ;
        };
        //生成token令牌
        /*********************************缓存token鉴权令牌********************************************/
        redisHelper.set(username + deviceId + '_pushTOken',push_Token + '@' + Date.now(),'',function(error,reply){
            if(!error){
                console.log('resdis save[token]...' = push_Token);
                res.status(200).json({
                    "result":true,
                    "host":"",
                    "port":8868,
                    "push_Token":push_Token,
                    "expired":dateHelper.getNowFor
                })
            }
        })
    })
}